# -*- coding: utf-8 -*-\
import datetime
import time,pytz
tz=pytz.timezone('Asia/Shanghai')
class utils_datetime:
    #时间戳转换为指定格式日期,farm="%Y-%m-%d %H:%M:%S"
    @staticmethod
    def strfmtime(timeStamp,format="%Y-%m-%d %H:%M:%S"):
        dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
        otherStyleTime = dateArray.strftime(format)
        return otherStyleTime

    #指定格式日期转换为时间戳
    @staticmethod
    def strpmtime(dateStr,format="%Y-%m-%d %H:%M:%S"):
        timeStamp=time.mktime(time.strptime(dateStr, format))
        return timeStamp

    #获取今日时间
    @staticmethod
    def todaytime(format="%Y-%m-%d %H:%M:%S"):
        now = datetime.datetime.now(tz)
        otherStyleTime = now.strftime(format)
        timeStamp=utils_datetime.strpmtime(otherStyleTime,format)
        return int(timeStamp)

    #获取今日前几日的时间
    @staticmethod
    def daystime(days):
        threeDayAgo = (datetime.datetime.now(tz) - datetime.timedelta(days))
        timeStamp = int(time.mktime(threeDayAgo.timetuple()))
        return timeStamp

    @staticmethod
    def next_time(dateStr,format="%Y-%m-%d"):
        now_time=datetime.datetime.strptime(dateStr, format)
        if len(dateStr)==10:
            dayAgo = (now_time + datetime.timedelta(1))
            return datetime.datetime(dayAgo.year,dayAgo.month,dayAgo.day).strftime(format)
        elif len(dateStr)==7:
            last_month = now_time.month + 1
            last_year = now_time.year
            if last_month == 13:
                last_month = 1
                last_year += 1
            last_day=1
            return datetime.datetime(last_year,last_month,last_day).strftime(format)
        elif len(dateStr)==4:
            last_year = now_time.year+1
            last_month = 1
            last_day=1
            return datetime.datetime(last_year,last_month,last_day).strftime(format)
        else:
            return



